package cn.bustack.middleware.dynamic.thread.pool.sdk.trigger.job;

import cn.bustack.middleware.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import cn.bustack.middleware.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.bustack.middleware.dynamic.thread.pool.sdk.registry.IRegistry;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/*
* 线程池数据上报任务
* */
public class ThreadPoolDataReportJob {

    private final Logger logger = LoggerFactory.getLogger(ThreadPoolDataReportJob.class);

    /*
    * 通过线程池服务拿数据
    * */
    private final IDynamicThreadPoolService dynamicThreadPoolService;

    /*
    * 通过注册中心上报
    * */
    private final IRegistry registry;


    public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }

    @Scheduled(cron = "0/20 * * * * ?")//标记一个方法，使其作为定时任务运行 每20秒时执行一次任务
    public void excReportThreadPoolList(){
        //查询到线程池列表
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPool(threadPoolConfigEntities);//上报线程池列表
        logger.info("动态线程池，上报线程池信息：{}", JSON.toJSONString(threadPoolConfigEntities));

        for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
            registry.reportThreadPoolConfigParameter(threadPoolConfigEntity);//上报线程池配置
            logger.info("动态线程池，上报线程池配置：{}", JSON.toJSONString(threadPoolConfigEntity));
        }
    }

}
